package com.demo.client;

import java.util.List;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.demo.thrift.Status;
import com.demo.thrift.Student;
import com.demo.thrift.ThriftMysqlService;

/**
 * 调用阻塞式多线程服务客户端
 *
 * @author HuangKe
 * @date 2017年3月7日
 * @company Copyright © 2007-2016 www.zgxcw.com All Rights Reserved
 */
public class BlockMultThreadClient {
	
	public static String ip = "localhost";
	public static int port = 8899;
	public static int time_out = 3000;
	
	public static void main(String[] args) {
		TTransport transport = null;
		try{
			transport = new TSocket(ip, port,time_out);
			TProtocol protocol = new TBinaryProtocol(transport);
			ThriftMysqlService.Client client = new ThriftMysqlService.Client(protocol);
			transport.open();
			
			//调用client方法
			//没有数据
			List<Student> list1 = client.queryAllUser();
			System.out.println(list1.size());
			//远程添加一个对象
			Student s = new Student(123, "Mr.HK", "123456", Status.OK);
			client.addUser(s);
			//再次查询列表
			List<Student> list2 = client.queryAllUser();
			System.out.println(list2.toString());
			//根据id查询对象
			System.out.println(client.queryOneUser(123));
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//记得关闭占用资源
			transport.close();
		}
	}
}
